//
//  CofirmOrder_Vm.swift
//  Pods
//
//  Created by eme on 2017/3/31.
//
//

import RxSwift
import UtilCore
import SwiftyJSON
import ModelProtocol
import NetWorkCore
import AddCarCore

/*
 owner:cy
 update:2016年12月19日17:15:05
 info: 进货单
 VC:   OrderForm_Vc
 */
class CofirmOrder_Vm :Base_Vm {
    
    /// 表示正在刷新中
    let loading = ActivityIndicator()
    /*
     接收返回的数据
     */
    let productItem = PublishSubject<OrderFormModel>()
    
    let createOrderSubject = PublishSubject<[String: AnyObject]>()
    let createOrderSuccess = PublishSubject<CreateOrderModel>()
    
    init(refreshTriger: Observable<(osCarts:NSString,iscart:String)>) {
        super.init()
        refreshTriger
            .map{ item in return OrderFormApi.cofirmorder(osCarts: item.osCarts, iscart: item.iscart) }
            .emeRequestApiForObj(OrderFormModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.productItem.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        ///觅客商城 删除购物车商品
        createOrderSubject
            .map{ OrderFormApi.createOrder($0) }
            .emeRequestApiForObj(CreateOrderModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.createOrderSuccess.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        
    }
    
}

